//
//  EmployeeTimeSearchViewController.swift
//  HuaYun
//
//  Created by enghou on 2019/1/11.
//  Copyright © 2019 xyxorigation. All rights reserved.
//

import UIKit

class EmployeeTimeSearchViewController: ProjectTimeSearchViewController {

    
    var username: String?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.tableView.height = 220
        // Do any additional setup after loading the view.
    }
    
    @objc override func searchAction()->Void{
        self.view.endEditing(true)
        self.delegate?.search(with: self.username, yearmonth: self.date, projname: self.projectname)
        self.dismiss()
    }
    
    override func reverseblock() {
        let animation = POPSpringAnimation.init(propertyNamed: kPOPViewFrame)
        animation?.beginTime = CACurrentMediaTime()
        animation?.toValue = NSValue.init(cgRect: CGRect.init(origin: CGPoint.init(x: 0, y: -220), size: CGSize(width: self.view.width, height: 220)))
        animation?.springSpeed = 50
        animation?.springBounciness = 5
        func complete(animation: POPAnimation?,complete: Bool)->Void{
            super.dismiss()
        }
        animation?.completionBlock = complete
        self.tableView.pop_add(animation, forKey: "t")
    }
    
    override func beginblock() {
        let animation = POPSpringAnimation.init(propertyNamed: kPOPViewFrame)
        animation?.beginTime = CACurrentMediaTime()
        animation?.toValue = NSValue.init(cgRect: CGRect.init(origin: CGPoint.init(x: 0, y: 0), size: CGSize(width: self.view.width, height: 220)))
        animation?.springSpeed = 50
        animation?.springBounciness = 5
        self.tableView.pop_add(animation, forKey: "t")
    }
    
    override func textFieldDidEndEditing(_ textField: UITextField) {
        if let usercell = self.tableView.cellForRow(at: IndexPath.init(row: 0, section: 0)) as? ProjectInputTableViewCell {
            if usercell.contentLabel.isEqual(textField) {
                self.username = textField.text
            }
        }
        
        if let usercell = self.tableView.cellForRow(at: IndexPath.init(row: 1, section: 0)) as? ProjectInputTableViewCell {
            if usercell.contentLabel.isEqual(textField) {
                self.projectname = textField.text ?? ""
            }
        }
    }
    
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 3
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.row == 0 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "ProjectInputTableViewCell") as! ProjectInputTableViewCell
            cell.contentLabel.delegate = self
            cell.contentLabel.placeholder = "人员姓名"
            return cell
        } else if indexPath.row == 1 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "ProjectInputTableViewCell") as! ProjectInputTableViewCell
            cell.contentLabel.delegate = self
            cell.contentLabel.placeholder = "项目名称"
            return cell
        } else {
            var cell = tableView.dequeueReusableCell(withIdentifier: "UITableViewCell")
            if cell == nil {
                cell = UITableViewCell.init(style: .value1, reuseIdentifier: "UITableViewCell")
                cell?.selectionStyle = .none
                cell?.textLabel?.font = UIFont.systemFont(ofSize: 17)
                cell?.detailTextLabel?.font = UIFont.systemFont(ofSize: 14)
                cell?.accessoryType = .disclosureIndicator
            }
            cell?.textLabel?.text = "年月:"
            return cell!
        }
    }
    
    override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        if indexPath.row == 0 {
            if let cel = cell as? ProjectInputTableViewCell {
                cel.contentLabel.text = self.username
            }
        } else if indexPath.row == 1 {
            if let cel = cell as? ProjectInputTableViewCell {
                cel.contentLabel.text = self.projectname
            }
        } else if indexPath.row == 2 {
            cell.textLabel?.text = "年份:"
            cell.detailTextLabel?.text = date
        }
    }
    
    override func endedit() {
        super.endedit()
        let cell = tableView.cellForRow(at: IndexPath.init(row: 2, section: 0))
        cell?.detailTextLabel?.text = self.yearPicker.date
    }

}
